package so.sao.shop.supplier.web.authorized;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.domain.User;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.SystemContext;
import so.sao.shop.supplier.pojo.input.authorized.RoleInput;
import so.sao.shop.supplier.pojo.output.authorized.RoleOut;
import so.sao.shop.supplier.service.authorized.RoleService;
import so.sao.shop.supplier.util.JwtTokenUtil;

import javax.validation.Valid;
import java.util.List;

/**
 * @author tengfei.zhang
 * @Title: RoleController
 * 角色接口
 * @create 2017-10-25 9:03
 **/
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/create")
    public Result<?> create(@Valid @RequestBody RoleInput roleInput) {
        User user = JwtTokenUtil.getCurrentUser();
        roleInput.setOperatorId(user.getId());
        roleInput.setIsAdmin(user.getIsAdmin());
        return roleService.save(roleInput);
    }

    @PutMapping("/update")
    public Result<?> update(@Valid @RequestBody RoleInput roleInput) {
        User user = JwtTokenUtil.getCurrentUser();
        roleInput.setOperatorId(user.getId());
        roleInput.setIsAdmin(user.getIsAdmin());
        return roleService.update(roleInput);
    }

    /**
     * 查询角色信息
     * @return 返回角色信息
     */
    @GetMapping("/search")
    public Result<?> search(RoleInput roleInput) {
        User user = JwtTokenUtil.getCurrentUser();
        roleInput.setUserId(user.getId());
        return roleService.searchRoles(roleInput);
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping(value = "/searchAllRole",method = RequestMethod.GET)
    public Result<?> searchAllRole() {
        User user = JwtTokenUtil.getCurrentUser();
        List<RoleOut> roleOuts = roleService.searchAllRole(user.getId());
        return Result.success("查询成功!",roleOuts);
    }

    @DeleteMapping("/delete/{roleId}")
    public Result<?> delete(@PathVariable Long roleId) {
        return roleService.deleteById(roleId);
    }

    @GetMapping("/get/{roleId}")
    public Result<?> get(@PathVariable Long roleId){
        return roleService.findById(roleId);
    }
}
